package com.shuhe.gauss;

import com.shuhe.metastore.DBSql;
import org.postgresql.core.BaseConnection;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;


public class SearchGauss {

    public static void main(String[] args) throws Exception {
        long begin = new Date().getTime();
        callSearch("ods_klj_sjb_mh",
                "10",
                "cert_no='110101198012225017'", "cert_no", "cert_no");
        long end = new Date().getTime();
        System.out.println("test:" + (end - begin));
    }


    public static ArrayList<String> callSearch(String tab_name, String maxNum,
                                               String whereStr, String orderStr,
                                               String fieldStr) {
        ArrayList<String> resp = null;
        BaseConnection destConn = null;
        try {
            //连接打开
            if (destConn == null || destConn.isClosed()) {
                destConn = (BaseConnection) DBSql.connect();
            }
            long begin = new Date().getTime();
            //数据查询
            resp = search(tab_name, maxNum, whereStr, orderStr, fieldStr, destConn);
            long end = new Date().getTime();
            System.out.println("callSearch:" + (end - begin));
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            //连接关闭
            try {
                destConn.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return resp;
    }


    public static ArrayList<String> search(String tab_name, String maxNum, String whereStr, String orderStr, String fieldStr, BaseConnection con) {
        ArrayList<String> lines = new ArrayList<String>();
        try {

            String sql = "select " + fieldStr + " from formal." + tab_name + " where " + whereStr + " order by " + orderStr + " limit " + maxNum;
            ResultSet rs = con.execSQLQuery(sql);
            int i = 0;
            while (rs.next()) {
                String value = "";
                for (int j = 0; j < rs.getMetaData().getColumnCount(); j++) {
                    value = value.concat(rs.getString(j + 1)).concat(",");
                }
                value = value.substring(0, value.length() - 1);
                lines.add(value);
            }
            System.out.println(lines);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return lines;

    }


}
